//
//  QDGuideTableViewCell.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/4/30.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit


/// 公告的tableViewCell
final class QDGuideTableViewCell: NWTableViewCell {
    
    /// cell的高度
    static var cellHeight: CGFloat = {
        return topHeight + CGFloat(46 + 14)
    }()
    
    /// 顶部图片的高度
    private static var topHeight: CGFloat = {
        return (UIScreen.main.ritl_width - 40) * CGFloat(160.0 / 335)
    }()
    
    /// 背景容器
    let containerView = UIImageView()
    /// 顶部的图片
    let topImageView = UIImageView()
    /// 标题
    let titleLabel = UILabel()
    /// 查看详情的标签
    let detailButton = UIButton()
    
    override func buildView() {
        super.buildView()
        
        selectionStyle = .none
        contentView.backgroundColor = 252.ritl_color
        
        containerView.backgroundColor = contentView.backgroundColor
        
        titleLabel.font = Font.regular.font(size: 16)
        titleLabel.textColor = QDColors.black
        titleLabel.text = "在线购票指南在线购票指南在线购票指南在线购票指南"
        
        detailButton.adjustsImageWhenHighlighted = false
        detailButton.titleLabel?.font = Font.regular.font(size: 12)
        detailButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: -16, bottom: 0, right: 16)
        detailButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 48, bottom: 0, right: -48)
        detailButton.setTitleColor(#colorLiteral(red: 0.3215686275, green: 0.8, blue: 0.4156862745, alpha: 1), for: .normal)
        detailButton.setTitle("查看详情", for: .normal)
        detailButton.setImage("guide_detail_arrow".namedImage, for: .normal)
        
        // 背景图
        containerView.image = "guide_background".namedImage?.resizableImage(withCapInsets: UIEdgeInsets(top: 7, left: 7, bottom: 7, right: 7))
        
        // 顶部图片
        topImageView.contentMode = .scaleAspectFill
        topImageView.clipsToBounds = true

        if #available(iOS 11.0, *) {
            topImageView.layer.cornerRadius = 8
            topImageView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]
        } else {
            // Fallback on earlier versions
            let shapeLayer = CAShapeLayer()
            let width = UIScreen.main.ritl_width - 40
            let size = CGSize(width: width, height: QDGuideTableViewCell.topHeight)
            let maskPath = UIBezierPath(roundedRect: CGRect(origin: .zero, size: size), byRoundingCorners: [.topRight,.topLeft], cornerRadii: CGSize(width: 8, height: 8))
            shapeLayer.frame = CGRect(origin: .zero, size: size)
            shapeLayer.path = maskPath.cgPath
            topImageView.layer.mask = shapeLayer
        }
        
        contentView.addSubview(containerView)
        containerView.addSubview(topImageView)
        containerView.addSubview(titleLabel)
        containerView.addSubview(detailButton)
        
        containerView.snp.makeConstraints { (make) in
            make.top.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().inset(13)
        }
        
        topImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(7)
            make.right.equalToSuperview().inset(7)
            make.top.equalToSuperview().offset(7)
            make.height.equalTo(topImageView.snp.width).multipliedBy(160.0 / 335)
        }
        
        detailButton.snp.makeConstraints { (make) in
            make.height.equalTo(17)
            make.width.equalTo(48 + 16)
            make.bottom.equalToSuperview().inset(21.5)
            make.right.equalToSuperview().inset(12)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(22)
            make.right.equalTo(detailButton.snp.left).offset(5)
            make.bottom.equalToSuperview().inset(19)
            make.left.equalToSuperview().offset(15)
        }
    }
}
